import Vue from 'vue'
import App from './index'

const app = new Vue(App)
app.$mount()

wx.login({
  success: res => {
    const appid = 'wxf17945b6e29268b5'
    const secret = 'f13d682e9b691e335bc8ee3cec965390'
    // 登录注册接口
    if (res.code) {
      // 调用服务端登录接口，发送 res.code 到服务器端换取 openId, sessionKey, unionId并存入数据库中
      wx.request({
        url: 'https://api.weixin.qq.com/sns/jscode2session?appid=' + appid + '&secret=' + secret + '&grant_type=authorization_code&js_code=' + res.code,
        header: {
          'content-type': 'application/json'
        },
        success: function (res) {
          wx.setStorage({
            key: 'openid',
            data: encodeURI(res.data.openid)
          })
        }
      })
    } else {
      console.log('登录失败！' + res.errMsg)
    }
  }
})
